<template>
  <div class="mask" v-show="showCart && total.count>0" @click="handleCartShowChange"></div>
  <div class="cart">
    <div class="product" v-show="showCart && total.count>0">
      <div class="product__header">
      <div
        class="product__header__all iconfont"
        :class="total.allChecked?'icon-check_checked':'icon-icons-'"
        @click="() => setCartItemsChecked(shopId)"
      >
        全选
      </div>
      <div class="product__header__clear">
        <span class="product__header__clear__btn"
          @click="() => cleanCartProducts(shopId)"
        >清空购物车</span>
      </div>
    </div>
    <template  v-for="item in total.productList" :key="item._id">
      <div 
        v-if="item.count>0" 
        class="product__item iconfont "
        :class="item.check?'icon-check_checked':'icon-icons-'"
        @click="()=>{changeCartItemChecked(shopId,item._id)}"
      >
        <img class="product__item__img" :src="item.imgUrl" />
        <div class="product__item__detail">
          <h4 class="product__item__title">{{ item.name }}</h4>
          <p class="product__item__price">
            <span class="product__item__yen">&yen;</span>{{ item.price }}
            <span class="product__item__origin">&yen;{{ item.oldPrice }}</span>
          </p>
        </div>
        <div class="product__number">
          <span
            class="product__number__minus iconfont icon-jianhao"
            @click.stop="() => { changeCartItemInfo(shopId, item._id, item, -1); }">
          </span>
          {{ item.count }}
          <span
            class="product__number__plus iconfont icon-jiahao-tianchong"
            @click.stop="() => { changeCartItemInfo(shopId, item._id, item, 1); }">
          </span>
        </div>
      </div>
    </template>
    </div>
    <div class="check">
      <div class="check__icon" @click="handleCartShowChange">
        <img
          src="http://www.dell-lee.com/imgs/vue3/basket.png"
          alt=""
          class="check__icon__img"
        />
        <div class="check__icon__tag">{{total.count}}</div>
      </div>
      <div class="check__info">
        总计：<span class="check__info__price">&yen; {{ total.sum }}</span>
      </div>
      <div class="check__btn" v-show="total.count>0">
        <router-link :to="{path:`/OrderConfirmation/${shopId}`}">
          去结算
        </router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from "vue";
import { useStore } from "vuex";
import { useRoute } from "vue-router";
import { useCommonCartEffect } from '../../effects/cartEffects';

// 获取购物车信息逻辑
const useCartEffect = (shopId) => {
  const store = useStore();
  const { changeCartItemInfo, total } = useCommonCartEffect(shopId);
  const changeCartItemChecked= (shopId,productId)=>{
    store.commit('changeCartItemChecked', { shopId, productId });
  }
  const cleanCartProducts = (shopId)=>{
    store.commit('cleanCartProducts',{shopId});
  }
  const setCartItemsChecked = (shopId)=>{
    store.commit('setCartItemsChecked',{shopId});
  }

  return {
      total,changeCartItemInfo,changeCartItemChecked,cleanCartProducts,setCartItemsChecked
    };
};


// 展示隐藏购物车逻辑
const toggleCartEffect = () => {
  const showCart = ref(false);
  const handleCartShowChange = ()=>{
    showCart.value=!showCart.value;
  }
  return {showCart,handleCartShowChange}
}

export default {
  name: "Cart",
  setup() {
    const route = useRoute();
    const shopId = route.params.id;
    const  { 
      total,changeCartItemInfo,changeCartItemChecked,cleanCartProducts,setCartItemsChecked
    } = useCartEffect(shopId); 
    const {showCart,handleCartShowChange} = toggleCartEffect();
    return {
      total,changeCartItemInfo,shopId,changeCartItemChecked,cleanCartProducts,setCartItemsChecked,showCart,handleCartShowChange
    };
  },
};
</script>

<style lang="scss" scoped>
.mask {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  background: rgba(0,0,0,0.5);
  z-index: 1;
}
.cart {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 2;
  background: #fff;
}
.product {
  overflow-y: scroll;
  flex: 1;
  background-color: #fff;
  &__header{
    display: flex;
    line-height: .52rem;
    border-bottom: .01rem solid #f1f1f1;
    font-size: .14rem;
    color: #333;
    .iconfont{
      margin-left: 0.17rem;
    }
    .icon-check_checked{
      color: #0091FF;
    }
    &__all {
      width: .64rem;
      margin-left: .18rem;
    }
    &__clear {
      flex: 1;
      margin-right: .16rem;
      text-align: right;
      &__btn {
        display: inline-block;
      }
    }
  }
  .icon-check_checked,
  .icon-icons-{
    // font-size: 0.18rem;
    line-height: 0.5rem;
  }
  .icon-check_checked{
    color: #0091FF;
  }
  &__item {
    position: relative;
    display: flex;
    padding: 0.12rem 0;
    margin: 0 0.16rem;
    border-bottom: .01rem solid #f1f1f1;
    &__detail {
      overflow: hidden;
    }
    &__img {
      width: 0.46rem;
      height: 0.46rem;
      margin-left: 0.1rem;
      margin-right: 0.1rem;
    }
    &__title {
      margin: 0;
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: #333333;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    &__price {
      margin: 0.06rem 0 0 0;
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: #E93B3B;
    }
    &__yen {
      font-size: 0.12rem;
    }
    &__origin {
      margin-left: 0.06rem;
      line-height: 0.2rem;
      font-size: 0.12rem;
      color: #999999;
      text-decoration: line-through;
    }
    .product__number {
      position: absolute;
      right: 0;
      bottom: 0.16rem;
      line-height: 0.18rem;
      color: #333;
      font-size: 0.12rem;
      &__minus {
        position: relative;
        top: 0.02rem;
        color: #666;
        margin-right: 0.05rem;
        // width: 0.2rem;
        // height: 0.2rem;
        // display: inline-block;
      }
      &__plus {
        position: relative;
        top: 0.02rem;
        color: #0091FF;
        margin-left: 0.05rem;
      }
    }
  }
}
.check {
  display: flex;
  height: 0.49rem;
  border-top: 0.01rem solid #f1f1f1;
  line-height: 0.49rem;
  &__icon {
    position: relative;
    width: 0.84rem;
    &__img {
      display: block;
      margin: 0.12rem auto;
      width: 0.28rem;
      height: 0.26rem;
    }
    &__tag {
      position: absolute;
      left: 0.46rem;
      top: 0.04rem;
      padding: 0 0.04rem;
      min-width: 0.2rem;
      height: 0.2rem;
      line-height: 0.2rem;
      background-color: #e93b3b;
      border-radius: 0.1rem;
      font-size: 0.12rem;
      text-align: center;
      color: #fff;
      transform: scale(0.5);
      transform-origin: left center;
    }
  }
  &__info {
    flex: 1;
    color: #333;
    font-size: 0.12rem;
    &__price {
      line-height: 0.49rem;
      color: #e93b3b;
      font-size: 0.18rem;
    }
  }
  &__btn {
    width: 0.98rem;
    background-color: #0091ff;
    text-align: center;
    font-size: 0.14rem;
    a {
      color: #fff;
      text-decoration: none;
    }
  }
}
</style>
